<?

/**
 * Debug function
 * Prints unformatted vars string
 */
function d() {
    print "<div class='message system'>DEBUG:<br />";
    foreach (func_get_args() as $arg) {
        print "<pre style='overflow: auto; z-index: 1001 !important; text-align: left !important; background: #000 !important; color: #0a0 !important; padding: 1ex !important; margin: 1ex !important; border: 1px solid #900 !important'>";
        print_r($arg);
        print "</pre>";
    }
    print "</div>";
}

function dd() {
    foreach (func_get_args() as $arg) {
        print "<pre style='overflow: auto; z-index: 1001 !important; text-align: left !important; background: #000 !important; color: #0a0 !important; padding: 1ex !important; margin: 1ex !important; border: 1px solid #900 !important'>";
        print_r($arg);
        print "</pre>";
    }
}

/**
 * Trantsating string in current language
 * @param string $str input string
 * @param array $replacers array whithin replases parameters
 * @return string output strung
 */
function t($str, $replacers = array()) {
    if (isset($GLOBALS['MESS'][$str]))
        $str = $GLOBALS['MESS'][$str];
    if (!empty($replacers)) {
        foreach ($replacers as $pattern => $replace) {
            $str = str_replace($pattern, t($replace), $str);
        }
    }
    return $str;
}

function IncludeClass($name, $dir = "/") {
    $exists = false;
    if (file_exists("$dir/$name.class.php")) {
        require_once "$dir/$name.class.php";
    }
    return $exists;
}

function getQueryString($params = array()) {
    $get = $_GET;
    $getStr = array();
    $array = array();

    foreach ($params as $key => $param) {
        if (isset($get[$key]))
            unset($get[$key]);
        $array[] = "$key=$param";
    }


    foreach ($get as $key => $val) {
        $getStr[] = "$key=$val";
    }

    $getStr = array_merge($getStr, $array);
    return "?" . implode("&", $getStr);
}

function pathToUri($path) {
    $path = str_replace("\\", "/", $path);
    return str_replace(ROOT_DIR, "", $path);
}

function check_session() {
    print "<input type=\"hidden\" name=\"session_id\" value=\"" . session_id() . "\">";
}

function set_action($action) {
    print "<input type=\"hidden\" name=\"action\" value=\"$action\">";
}

function check_action($action, $type = false) {
    $ok = false;
    switch ($type) {
        case 'get':
            $ok = (isset($_GET['action']) && $_GET['action'] == $action);
            break;
        case 'post':
            $ok = (isset($_POST['action']) && $_POST['action'] == $action);
            break;
        default : $o = (isset($_REQUEST['action']) && $_REQUEST['action'] == $action);
    }
    return $ok;
}

function GetOplionsList($data, $var, $val, $select_key = array(), $level = 0) {
    $str = "";
    foreach ($data as $item) {
        $selected = false;
        if(!empty($select_key)){
            $selected = $item[$select_key[0]] == $select_key[1]  ? "selected" : false;
        }
        $valArr = explode(":", $val);
        $value = $item;
        foreach ($valArr as $subarr) {
            $value = $value[$subarr];
        }
        $str .= "<option $selected value=\"".$item[$var]."\">".str_repeat("- ", $level).$value."</option>";
        if(!empty($item['children'])){
            $str .= GetOplionsList($item['children'], $var, $val, $select_key, $level+1);
        }
    }
    //$str .= "<option value=\"".$item[$select_key['needle']]."\">".$item[$value_key]."</option>";
    return $str;
}
